<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * MY_Loader class extends the core CI_Loader class.
 *
 * @author  Eric 'Aken' Roberts <eric@cryode.com>
 * @link    https://github.com/cryode/CodeIgniter_Smarty
 * @version 1.1.0
 * @license MIT
 */

class MY_Controller extends CI_Controller {

    protected $data;
    protected $pager = array();

    function __construct () {
        parent::__construct();
        $this->lang->load('english', 'english');
        $this->config->set_item('language', 'english');
        
        $lang = $this->input->get('lang');
        if(!empty($lang)){
            $this->session->unset_userdata('language');
            $this->session->set_userdata('language' , $lang);
        }
        
        $langSess = $this->session->userdata('language');
        if(!empty($langSess)){
            if($langSess == 'jp'){
                $this->lang->load('japanese', 'japanese');
                $this->config->set_item('language', 'japanese');
                $this->data['now_lang'] = $langSess;
            }
        }
        
        $this->data['base_url'] = $this->config->item('base_url');
        $this->data['title']    = 'Shop';
        $this->data['config']   = $this->config->config;
        $this->data['lang']     = $this->lang->language;
        $this->pager['per_page']= 10;
        $this->doValidToken();
//         $this->setTokenTo();
    }

    public function view($template, $data = array(), $return = FALSE)
    {
        // Get the CI super object, load related library.
        $CI =& get_instance();
        $CI->load->library('smartytpl');

        // Add extension to the filename if it's not there.
        $ext = '.' . ltrim($CI->config->item('smarty_template_ext'), '.');

        if (substr($template, -strlen($ext)) !== $ext)
        {
            $template .= $ext;
        }

        // Make sure the file exists first.
        if ( ! $CI->smartytpl->templateExists($template))
        {
            show_error('Unable to load the template file: ' . $template);
        }

        // Assign any variables from the $data array.
        $CI->smartytpl->assign_variables($data);

        /*
         Smarty has two built-in functions to rendering templates: display()
        and fetch(). We're going to	use only fetch(), since we want to take
        the template contents and either return them or add them to
        CodeIgniter's output class. This lets us optionally take advantage
        of some of CI's built-in output features.
        */

        try
        {
            $output = $CI->smartytpl->fetch($template);
        }
        catch (Exception $e)
        {
            show_error(htmlspecialchars_decode($e->getMessage()), 500, 'Smarty Exception');
        }

        // Return the output if the return value is TRUE.
        if ($return === TRUE)
        {
            return $output;
        }

        // Otherwise append to output just like a view.
        $CI->output->append_output($output);
    }

    public function insert($table = null , $params = array()) {
        if (empty($table) || empty($params)) {
            return false;
        }

        $params["regist_datetime"] = date("Y-m-d H:i:s");
        $params["lastup_datetime"] = date("Y-m-d H:i:s");
        $params["disable"]         = 0;
        $this->db->insert($table, $params);
        return true;
    }

    public function update($table = null , $params = array(), $where = array()) {
        if (empty($table) || empty($params)) {
            return false;
        }

        $params["lastup_datetime"] = date("Y-m-d H:i:s");
        $where["disable"] = 0;
        $this->db->update($table, $params, $where);
        return true;
    }

    public function delete($table = null ,$where = array()) {
        if (empty($table) || empty($where)) {
            return false;
        }

        $params = array();
        $params["lastup_datetime"] = date("Y-m-d H:i:s");
        $params["disable"] = 1;
        $where["disable"] = 0;
        $this->db->update($table, $params, $where);
        return true;
    }

    public function offset() {
        if ((int)$this->pager["cur_page"] <= 0) {
            $this->pager["cur_page"] = 1;
        }
        return (int)($this->pager["cur_page"] - 1) * $this->pager["per_page"];
    }

    public function doValidToken($is_admin = false)
    {
        $transid = false;
        if(!empty($this->session->userdata['transid'])){
            $transid = $this->session->userdata['transid'];
        }

        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $req = false;
            if(!empty($_REQUEST['transid'])){
                $req = $_REQUEST['transid'];
            }
            $ret = $req === $transid;
            if ($ret === false) {
                $this->session->unset_userdata('transid');
            }
            if (!$ret) {
                if ($is_admin) {
//                     DispError(INVALID_MOVE_ERRORR);
                } else {
//                     DispSiteError(PAGE_ERROR, '', true);
                }
                exit();
            }
        }
        if (empty($transid)) {
            $this->session->set_userdata('transid',uniqid(rand(), true));
        }
        $this->data['transactionid'] = $this->session->userdata['transid'];
    }

    public function setPager($arrPager) {
        foreach ($arrPager as $key => $value) {
            $this->pager[$key] = $value;
            if($key == "cur_page" && !is_numeric($value)){
                $this->pager[$key] = 0;
            }
        }
    }
    
    public function getPagerTpl($admin_flg = true) {
        $this->pagination->initialize($this->pager);
        $page_display = $this->pagination->create_links();
        if($admin_flg){
            return MY_Helper::pager_tpl($page_display);
        }
        return $page_display;
    }

    public function setValue($key = '', $value = '') {
        $_POST[$key] = $value;
    }
}

class MY_Admin extends MY_Controller {

    protected $data;

    function __construct () {
        parent::__construct();
        $this->lang->load('japanese', 'japanese');
        $this->config->set_item('language', 'japanese');
        
        $this->data['title']    = "ATTO 管理機能";
        $this->pager['anchor_class']= "class='btn'";
        $this->load->helper('url');

        if($this->session->userdata('logined') != 'OK'){
            redirect('/admin','refresh');
            exit;
        }
        $this->data['user_login'] = $this->session->userdata('user_login');
    }

    public function getInputData() {
        return $this->input->post(NULL, TRUE);
    }
}
/* End of file Loader.php */
/* Location: ./application/core/Loader.php */
